public class Manage3 {
    private static volatile Manage3 INSTANCE;
    private static int id = 0;

    private Manage3() {
        id++;
    }

    // 加锁！
    public static Manage3 getInstance() {
        if (INSTANCE == null) {
            synchronized (Manage2.class) {
                if (INSTANCE == null) {
                    try {
                        Thread.sleep(1); // 假装new变得很慢，机器卡住了
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    INSTANCE = new Manage3();
                }
            }
        }
        return INSTANCE;
    }

    @Override
    public String toString() {
        return "id=" + id;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Manage3.getInstance().hashCode());
            }
            ).start();
        }
    }
}
